package tool.plot

import java.io.File
import implicits.Implicits._
import play.api.data.FormBinding
import play.api.mvc.{AnyContent, Request}
import shared.Pojo._
import shared.pojo.mission
import tool.Pojo._
import tool.Tool
import tool.plot.AllPlot.{MyRPlotB, RPlotB}
import upickle.default._
import utils.Utils

/** Created by yz on 27/9/2020
  */
object Pca2D extends Pca2DT with MyRPlotB {

  val plotO = mission.Pca2DPlot

  override def getCommand(implicit configJson: ujson.Value, workspaceDir: File): String = {
    s"""
       |${Tool.rScript} ${new File(Tool.rPath, "pca.R").unixPath} --pc ${plotO.configFileName}
       |convert  -density 300 PCA_Score_2D.pdf  PCA_Score_2D.png
       |""".stripMargin
  }

  def getCommand(workspaceDir: File, idStr: String): String = {
    implicit val configJson = ujson.read(configFile(workspaceDir, idStr))
    implicit val wd = workspaceDir
    getCommand
  }

  override def getWorkspaceDir(workspaceDir: File, idStr: String,prefix:String=""): File = {
    new File(workspaceDir, s"${idStr}")
  }

  def configFile(workspaceDir: File, prefix: String) = {
    val execWorkspaceDir = getWorkspaceDir(workspaceDir, prefix)
    new File(execWorkspaceDir, plotO.configFileName)
  }

  def initConfigFile(workspaceDir: File, idStr: String) = {
    val configData = plotO
      .Request()
    Utils.t2ConfigFileWithDefault(configData, configFile(workspaceDir, idStr))
  }

  override def producePlotConfigFile(
      workspaceDir: File
  )(implicit request: Request[AnyContent], formBinding: FormBinding): Any = {
    val data = getData
    Utils.t2ConfigFileWithDefault(data, configFile(workspaceDir, data.idStr))
  }

  def getData(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    Tool.transformRq.transform(reader[plotO.Request])
  }

}
